package remote

import (
	mes "ez/apps/mes/document"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

func GetPlans() {

	for i := 0; i < 50; i++ {
		req := NewEzRequest()
		resp, e := req.PostMessage("http://192.168.7.3:666/api/mes/admin/Plan/list", ss.M{
			"_where": ss.M{},
			"page":   i + 1,
			"limit":  100,
			//"_d":    1,
		})
		if e != nil {
			println(e)
			return
		}
		msg, e := NewMessageFrom(resp)
		if e != nil {
			println(e)
			return
		}
		if msg == nil {
			println("msg is nil")
			return
		}
		wanted := struct {
			Lists []*Plan `json:"lists"`
		}{}
		e = msg.GetData(&wanted)
		if e != nil {
			println(e.Error())
			return
		}
		crud := mes.NewPlanCrud()
		for _, plan := range wanted.Lists {
			find, err := crud.FindOneBy(ss.M{"sn": plan.Sn}, nil)
			if err != nil {
				find.Sn = plan.Sn
				if plan.Task != nil {
					find.Product = plan.Task.Name
				} else {
					find.Product = "未找到"
				}
				err = find.Save()
				if err != nil {
					println(err.Error())
				}
			}
		}
		if len(wanted.Lists) == 0 {
			break
		}
		time.Sleep(2 * time.Second)
	}
}

type Plan struct {
	Id          int        `json:"id" gorm:"primarykey"`
	Sn          string     `json:"sn" gorm:"size:255;" flag:""`
	Name        string     `json:"name" gorm:"size:255;" flag:""`
	StartAt     *time.Time `json:"startAt" gorm:"null;" flag:""`
	TargetAt    *time.Time `json:"targetAt" gorm:"null;" flag:""`
	FinishAt    *time.Time `json:"finishAt" gorm:"null;" flag:""`
	NumPlan     int        `json:"numPlan" gorm:"" flag:""`
	NumDone     int        `json:"numDone" gorm:"" flag:""`
	NumNeed     int        `json:"numNeed" gorm:"" flag:""`
	NumFeed     int        `json:"numFeed" gorm:"" flag:""`
	TypeId      int        `json:"typeId"`
	GroupId     int        `json:"groupId"`
	Flag        string     `json:"flag" gorm:"size:255;" flag:""`
	TaskId      int        `json:"taskId"`
	Task        *Task      `json:"task" gorm:"" flag:"M2O"`
	ExecutorId  int        `json:"executorId"`
	RoutingId   int        `json:"routingId"`
	StatusId    int        `json:"statusId"`
	IsFinish    bool       `json:"isFinish" gorm:"" flag:""`
	IsStop      bool       `json:"isStop" gorm:"" flag:""`
	ParentId    int        `json:"parentId"`
	Parent      *Plan      `json:"parent" gorm:"" flag:"M2O"`
	Sons        []*Plan    `json:"sons" gorm:"foreignKey:parent_id;" flag:"O2M"`
	IsSonsLiner bool       `json:"isSonsLiner" gorm:"" flag:""`
	CreateAt    *time.Time `json:"createAt" gorm:"null;autoCreateTime;" flag:""`
	CreateBy    int        `json:"createBy" gorm:"" flag:""`
	UpdateAt    *time.Time `json:"updateAt" gorm:"null;autoCreateTime;autoUpdateTime;" flag:""`
	DeleteAt    *time.Time `json:"deleteAt" gorm:"null;" flag:""`
	IsDelete    bool       `json:"isDelete" gorm:"" flag:""`
	IsLock      bool       `json:"isLock" gorm:"" flag:""`
}
type Task struct {
	Id     int     `json:"id" gorm:"primarykey"`
	Name   string  `json:"name" gorm:"size:255;" flag:""`
	Info   string  `json:"info" gorm:"size:255;" flag:""`
	OutId  int     `json:"outId" gorm:"" flag:""`
	OutSrc string  `json:"outSrc" gorm:"size:255;" flag:""`
	IsOn   bool    `json:"isOn" gorm:"" flag:""`
	Weight float64 `json:"weight" gorm:"digits(12); decimals(4);" flag:""`
}
